<?php
if(!isset($intergrated))
{
header('Content-Type: text/xml');
header('Cache-control: no-cache');
}

include_once("../store/other/util/functs.php");
include_once("../store/other/util/util.php");


//load database information
$db_info = get_database_info();

$link = mysql_connect($db_info['db_server'], 'root', $db_info['db_password']); //right now we are hardcoded..
if (!$link) 
{
   die('Could not connect: ' . mysql_error());
}

mysql_select_db($db_info["db_name"]);

$property_name = $_GET['propertyName'];
$data_id = $_GET['dataId'];
$value = $_GET['value'];
$add_remove = $_GET['add_remove'];
databinding_updater($property_name, $data_id, $value, $add_remove);

$funct_name = $_GET['funct_name'];
$input_data = $_GET;
$input_vars = load_input_vars_from_data($input_data);
$output_vars = NULL;
//input some standard sys variables
$input_vars['sys']['link'] = $link;
$input_vars['sys']['intergrated'] = $intergrated;

$just_loaded = false;
if (!isset($funct_name) ||$funct_name == "")
{
$just_loaded = true;
$output_vars = page_on_load ($input_vars);
}
$output_vars['sys']['link'] = NULL;
$php_return_data = echo_differences_of($input_vars, $output_vars, $just_loaded);
if(!$intergrated)
{
echo $php_return_data;
}
mysql_close($link);
// Handling the onLoad of Page0 
function Page0_onLoad ($args)
{
$query = "SELECT  FROM ";
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	$args['table'][''] = array(); //initializing appropriately
	// see if any rows were returned
	if (mysql_num_rows($result) > 0) 
	{
		while($row = mysql_fetch_row($result)) 
		{
			// set the text
			$id = $row[0]; // this will give an id
			$args['table'][''][$id] = array("id" => $id);
		}
	} 
return $args;

} // end of the function Page0_onLoad

// Handling the common page_on_load function 
function page_on_load ($input_vars) 
{
// call function for Page0 
$output_vars = Page0_onLoad ($input_vars);
$input_vars = $output_vars; //now input will be last outputs;)
$output_vars["table"] = 
       update_databound_properties(NULL, $input_vars, 1);
return $output_vars;

} // end of the function page_on_load

// the following function will be called by util.php
// to update the values of databound properties
function update_databound_properties($diff_vars, $args, $starter)
{

$table_vars = $args['table'];
if ($starter == 1 || 
  ($diff_vars["table"][""] != NULL && 
   $diff_vars["table"][""]["changed"] == 1))
{
$query = "SELECT  FROM  WHERE -1";
if(is_array($args['table']['']))
{
$data_table = array_keys($args['table']['']); //just give another temporary_name
$query .= "";
$total = count($data_table);
if($total != 0)
{
$query .="(";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= " = ". $data_value;
	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
}
else
{
}
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if($table_vars[""] == NULL)
	{
		$table_vars[""] = array();
	}
// see if any rows were returned
if (mysql_num_rows($result) > 0) 
{
	while($row = mysql_fetch_row($result)) 
	{
		// retrieve the id
		$id = $row[0];
		
		if($table_vars[""][$id] == NULL)
		{
			$table_vars[""][$id] = array();
		}
		$table_vars[""][$id]= $row[1]; //since the player id is retrieved as row[0]
	}
} 

} //close the most outer if

if ($starter == 1 || 
  ($diff_vars["table"][""] != NULL && 
   $diff_vars["table"][""]["changed"] == 1))
{
$query = "SELECT  FROM  WHERE -1";
if(is_array($args['table']['']))
{
$data_table = array_keys($args['table']['']); //just give another temporary_name
$query .= "";
$total = count($data_table);
if($total != 0)
{
$query .="(";
$i = 0;
foreach($data_table as $data_value) // these are typically ids
{
	//complete the query dynamically..
	$query .= " = ". $data_value;
	$i ++;
	if($total != $i)
	{
		$query .= " OR ";
	}
}
$query .=")";
}
else
{
}
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query ".mysql_error()); 
	if($table_vars[""] == NULL)
	{
		$table_vars[""] = array();
	}
// see if any rows were returned
if (mysql_num_rows($result) > 0) 
{
	while($row = mysql_fetch_row($result)) 
	{
		// retrieve the id
		$id = $row[0];
		
		if($table_vars[""][$id] == NULL)
		{
			$table_vars[""][$id] = array();
		}
		$table_vars[""][$id]= $row[1]; //since the player id is retrieved as row[0]
	}
} 

} //close the most outer if


return $table_vars;
}
//finally the function for databinding_updater..
//responsible for update the databound properties
function databinding_updater($property_name, $data_id, $value, $add_remove)
{
if($property_name == NULL)
{
 return;
}

$update_query = NULL;

// update query for Label3_text
if($property_name == "Label3_text")
{
  $update_query = "UPDATE  SET  = ('{$value}') WHERE -1='{$data_id}'";
}
// update query for _check_CheckBoxGroup2_sec
else if($property_name == "_check_CheckBoxGroup2_sec")
{
  $update_query = "UPDATE  SET  = ('{$value}') WHERE -1='{$data_id}'";
}

if($update_query != NULL)
{
 $result = mysql_query($update_query) or die ("Error in query: $update_query ".mysql_error());
}
}

?>
